<template>
  <div class="app-container">
    <div style="text-align: center;margin-bottom: 30px">
      版本{{ createStatus ? '新增' : '更新' }}
    </div>
    <el-row :gutter="10">
      <el-form :model="appForm" :rules="apprules" ref="appForm" label-width="110px" class="form app_form">
        <el-form-item label="应用唯一号" prop="appKey">
          <el-input v-model="appForm.appKey" placeholder="请输入应用的唯一号"></el-input>
        </el-form-item>
        <el-form-item label="版本名" prop="versionName">
          <el-input v-model="appForm.versionName" placeholder="请输入应用版本名，例如：1.0.24"></el-input>
        </el-form-item>
        <el-form-item label="版本号" prop="versionCode">
          <el-input v-model="appForm.versionCode" oninput="value=value.replace(/[^\d]/g,'')"
                    placeholder="请输入应用版本号，例如：24"></el-input>
        </el-form-item>
        <el-form-item label="是否强制更新" prop="updateStatus">
          <el-switch v-model="appForm.updateStatus" :inactive-value="1" :active-value="2"></el-switch>
        </el-form-item>
        <el-form-item label="更新内容" prop="modifyContent">
          <el-input type="textarea" v-model="appForm.modifyContent" placeholder="请输入更新日志..."></el-input>
        </el-form-item>
        <el-form-item label="APK文件">
          <el-upload
              class="upload-demo"
              ref="upload"
              :headers="headers"
              :action="uploadImg"
              :data="{type:'avatar'}"
              accept=".apk"
              :before-upload="beforeAvatarUpload"
              :on-success="handleAvatarSuccess"
              :file-list="fileList">
            <el-button slot="trigger" size="small" type="success">选取.apk文件</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
    </el-row>


    <div slot="footer" style="text-align: center" class="dialog-footer">
      <el-button @click="cancelApk">取 消</el-button>
      <el-button :loading="addLoading" v-if="createStatus" type="primary" @click="createApk">创 建
      </el-button>
      <el-button :loading="addLoading" v-else type="primary" @click="updateApk">修 改</el-button>
    </div>
  </div>
</template>

<script>
import {uploadImg} from "@/api/common";
import {getToken} from "@/utils/auth";
import {
  addVersionInfo,
  updateVersion,
  getDataById
} from "@/api/apk";

export default {
  name: "apkEdit",
  components: {},
  data() {
    return {
      uploadImg,
      fileList: [
      ],
      appForm: {
        appKey: "",
        appSize: 0,
        versionName: "",
        downloadUrl: "",
        versionCode: "",
        modifyContent: "",
        updateStatus: 1,
      },
      apprules: {
        appKey: [{
          required: true,
          message: "请输入应用唯一号",
          trigger: "blur"
        }],
        versionName: [{
          required: true,
          message: "请输入版本名",
          trigger: "blur"
        }],
        versionCode: [{
          required: true,
          message: "请输入版本号",
          trigger: "blur"
        }]
      },
      addLoading: false,
      enterpriseList: [],
      enterpriseDominantList: [],
      roleList: [],
      imgFlag: false,
      imgUploadPercent: 0,
      //选中岗位编码数组
      postCodeList:[],
      //岗位列表
      postList:[]
    }
  },
  computed: {
    createStatus() {
      return this.$route.params.ucId == '0'
    },
    headers() {
      return {
        'Authorization': getToken()
      }
    }
  },
  created() {
    if (!this.createStatus) {
      let ucId = this.$route.params.ucId
      this.getDataById(ucId)
    }
  },
  methods: {
    getDataById(val) {
      getDataById({versionId: val}).then(res => {
        this.appForm = res.data.data
      }).catch(res => {

      })
    },
    handleChange(value){
      console.log(value)
    },
    cancelApk() {
      window.history.back()
    },
    createApk() {
      this.addLoading = true
      this.$refs['appForm'].validate(valid => {
        if (valid) {
          debugger
          addVersionInfo(this.appForm).then(res => {
            this.addLoading = false
            this.$notify.success({
              title: '成功',
              message: "添加新版本apk成功"
            })
            this.cancelApk()
          }).catch(res => {
            this.addLoading = false
            this.$notify.error({
              title: '失败',
              message: res.msg
            })
          })
        } else {
          this.addLoading = false
          console.log("表单验证不通过")
        }
      })
    },
    updateApk() {
      this.addLoading = true
      this.$refs['appForm'].validate(valid => {
        if (valid) {

          updateVersion(this.appForm).then(res => {
            this.addLoading = false
            this.$notify.success({
              title: '成功',
              message: "更新APK成功"
            })
            this.cancelApk()
          }).catch(res => {
            this.addLoading = false
            this.$notify.error({
              title: '失败',
              message: res.msg
            })
          })
        } else {
          this.addLoading = false
          console.log("表单验证不通过")
        }
      })
    },
    beforeAvatarUpload(file) {
      this.appForm.apkSize = file.size / 1024 / 1024;
      return true;
    },
    handleAvatarSuccess(res, file) {
      this.imgFlag = false;
      debugger
      this.appForm.downloadUrl = res.data;
    },
    uploadImgProcess(event, file, fileList) {
      this.imgFlag = true;
      this.imgUploadPercent = file.percentage.toFixed(0);
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 149px;
  height: 149px;
  line-height: 149px;
  text-align: center;
}

.avatar {
  width: 149px;
  height: 149px;
  display: block;
}
</style>
